/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import entities.Event;
import entities.Notification;
import entities.OutdoorEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Alberto, Alessandra
 */
@Startup
@Singleton
public class NotificationScheduler {
    
    @PersistenceContext
    EntityManager em;
    
    @EJB
    NotificationManager nm;
    
    @EJB
    WeatherManager wm;
    
    @Schedule()
    public void generateOneDayNotification () {
        ArrayList<OutdoorEvent> tomorrowevents = new ArrayList<>();
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        tomorrowevents.addAll(em.createNamedQuery("findOutdoorEventByDate" , OutdoorEvent.class)
            .setParameter("Edate", cal.getTime())
            .getResultList());
        Iterator<OutdoorEvent> iter = tomorrowevents.iterator();
        while (iter.hasNext()) {
            OutdoorEvent event = iter.next();
            if (event.isBadWeather())
                nm.createOneDayNotifications(event);
        }
    }
    
    @Schedule()
    public void generateThreeDaysNotification () {
        ArrayList<OutdoorEvent> futureevents = new ArrayList<>();
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, 3);
        futureevents.addAll(em.createNamedQuery("findOutdoorEventByDate" , OutdoorEvent.class)
            .setParameter("Edate", cal.getTime())
            .getResultList());
        Iterator<OutdoorEvent> iter = futureevents.iterator();
        while (iter.hasNext()) {
            OutdoorEvent event = iter.next();
            if (event.isBadWeather())
                nm.createThreeDaysNotification(event, wm.computeProposedDate(event));
        }
    }
    
    @Schedule()
    public void cleanEvents () {
        ArrayList<Event> events = new ArrayList<>();
        events.addAll(em.createNamedQuery("findAllEvents", Event.class)
            .getResultList());
        Iterator<Event> iter = events.iterator();
        while (iter.hasNext()) {
            Event e = iter.next();
            if (e.getParticipants().isEmpty() && e.getDeletenotifications().isEmpty()) {
                if (e instanceof OutdoorEvent) {
                    OutdoorEvent oe = (OutdoorEvent) e;
                    oe.getBadweather().clear();
                    oe.setWeatherforecast(null);
                    em.merge(oe);
                }
                em.createNamedQuery("removeEvent", Event.class)
                        .setParameter("Eid", e.getId())
                        .executeUpdate();
            }
        }
    }
    
}
